package com.ssd.controller;

import com.ssd.entity.PartnerEntity;
import com.ssd.entity.PartnerEntity;
import com.ssd.entity.VmTypeEntity;
import com.ssd.entity.vo.PartnerVo;
import com.ssd.service.PartnerService;
import com.ssd.utils.StringUtils;
import com.ssd.view.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/partner")
@Slf4j
public class PartnerController {

    @Resource
    private PartnerService partnerService;

    @GetMapping("/list")
    public BaseResponse<List<PartnerVo>> getPartnerPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10000") int pageSize,
            @RequestParam(required = false) String partnerName){
        return new BaseResponse(partnerService.getPartnerList(pageNum, pageSize,partnerName));
    }


    @GetMapping("/{partnerId}")
    public BaseResponse<PartnerVo> getPartnerDetail(@PathVariable("partnerId") Long partnerId) {
        return new BaseResponse(partnerService.selectPartnerByPartnerId(partnerId));
    }


    @PostMapping
    public BaseResponse<String> addPartner(@RequestBody PartnerEntity partner) {
        return new BaseResponse(partnerService.insertPartner(partner));
    }


    @PutMapping
    public BaseResponse<String> updatePartner(@RequestBody PartnerEntity partner) {
        return new BaseResponse(partnerService.updatePartner(partner));
    }


    @DeleteMapping("/{partnerIds}")
    public BaseResponse<String> batchDeletePartner(@PathVariable("partnerIds") String classIdsStr) {
        Long[] classIds = StringUtils.isEmpty(classIdsStr) ?
                new Long[0] :
                Arrays.stream(classIdsStr.split(","))
                        .map(Long::parseLong)
                        .toArray(Long[]::new);
        return new BaseResponse(partnerService.deletePartnerByPartnerIds(classIds));
    }

    @PutMapping("/resetPwd/{id}")
    public BaseResponse<String> resetPwd(@PathVariable Long id){//1. 接收请求参数
        //2. 创建合作商对象
        PartnerEntity partner = new PartnerEntity();
        partner.setId(id);// 设置id
        partner.setPassword("123456");// 设置加密后的初始密码
        //3. 调用service更新密码
        return new BaseResponse(partnerService.updatePartner(partner));
    }
}